package com.google.code.virtualhockey.vhx.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility class providing methods that help dealing with JavaScriptObjects.
 */
public class JsUtils {

  /** Private constructor to avoid instantiation. */
  private JsUtils() {}

  /**
   * Converts a JsArray object into a Java List.
   *
   * @param array the array to be transformed
   * @param <T> the type of Object that is in the array
   * @return the List containing exactly the same elements as the array
   */
  public static <T extends JavaScriptObject> List<T> toList( JsArray<T> array ) {
    ArrayList<T> list = new ArrayList<T>();

    for ( int i = 0; i < array.length(); i++ )
      list.add( array.get( i ) );

    return list;
  }
}
